package me.aartikov.alligator.helpers;


import me.aartikov.alligator.Screen;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;


/**
 * Helper class for storing screen class information in activities and fragments.
 */
public class ScreenClassHelper {
    private static final String KEY_SCREEN_CLASS_NAME = "me.aartikov.alligator.KEY_SCREEN_CLASS_NAME";
    private static final String KEY_PREVIOUS_SCREEN_CLASS_NAME = "me.aartikov.alligator.KEY_PREVIOUS_SCREEN_CLASS_NAME";

    private Map<Class<? extends Ability>, Class<? extends Screen>> mActivityMap = new HashMap<>();     // this map is used when there are no screen class information in an activity intent
    private Map<Integer, Class<? extends Screen>> mRequestCodeMap = new LinkedHashMap<>();

    public void putScreenClass(Intent intent, Class<? extends Screen> screenClass) {
//		intent.putExtra(KEY_SCREEN_CLASS_NAME, screenClass.getName());

        intent.setParam(KEY_SCREEN_CLASS_NAME, screenClass.getName());
    }

    @SuppressWarnings("unchecked")

    public Class<? extends Screen> getScreenClass(Ability activity) {
//		String className = activity.getIntent().getStringExtra(KEY_SCREEN_CLASS_NAME);
        String className = activity.getIntent().getStringParam(KEY_SCREEN_CLASS_NAME);
        Class<? extends Screen> screenClass = getClassByName(className);
        return screenClass != null ? screenClass : mActivityMap.get(activity.getClass());
    }

    public void putScreenClass(MiFraction fragment, Class<? extends Screen> screenClass) {
//		Bundle arguments = fragment.getArguments();
//        if (arguments == null) {
//            arguments = new Bundle();
//            fragment.setArguments(arguments);
//        }
//        arguments.putString(KEY_SCREEN_CLASS_NAME, screenClass.getName());

        IntentParams intentParams = fragment.getIntentParams();
        if (intentParams == null) {
            intentParams = new IntentParams();
        }

        intentParams.setParam(KEY_SCREEN_CLASS_NAME, screenClass.getName());
        fragment.setIntentParams(intentParams);

    }

    @SuppressWarnings("unchecked")

    public Class<? extends Screen> getScreenClass(MiFraction fragment) {
//        if (fragment.getArguments() == null) {
//            return null;
//        }
//
//        String className = fragment.getArguments().getString(KEY_SCREEN_CLASS_NAME);
        if (fragment.getIntentParams() == null) {
            return null;
        }

        String className = (String) fragment.getIntentParams().getParam(KEY_SCREEN_CLASS_NAME);
        return (Class<? extends Screen>) getClassByName(className);
    }

    public void putPreviousScreenClass(Intent intent, Class<? extends Screen> screenClass) {
//        intent.putExtra(KEY_PREVIOUS_SCREEN_CLASS_NAME, screenClass.getName());
        intent.setParam(KEY_PREVIOUS_SCREEN_CLASS_NAME, screenClass.getName());
    }

    @SuppressWarnings("unchecked")

    public Class<? extends Screen> getPreviousScreenClass(Ability activity) {
        String className = activity.getIntent().getStringParam(KEY_PREVIOUS_SCREEN_CLASS_NAME);
        return getClassByName(className);
    }


    public Class<? extends Screen> getScreenClass(int requestCode) {
        return mRequestCodeMap.get(requestCode);
    }

    public void addActivityClass(Class<? extends Ability> activityClass, Class<? extends Screen> screenClass) {
        if (!mActivityMap.containsKey(activityClass)) {
            mActivityMap.put(activityClass, screenClass);
        }
    }

    public void addRequestCode(int requestCode, Class<? extends Screen> screenClass) {
        if (!mRequestCodeMap.containsKey(requestCode)) {
            mRequestCodeMap.put(requestCode, screenClass);
        }
    }


    private static Class getClassByName(String className) {
        if (className == null || className.isEmpty()) {
            return null;
        }

        try {
            return Class.forName(className);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }
}
